<?php
/**
 * ReportsController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-04-24 09:46:54
 * @modified    2023-04-24 09:46:54
 *
 */

namespace App\Http\Controllers;

use App\Repositories\ReportsRepo;
use Illuminate\Http\Request;

class ReportsController extends BaseModuleController
{
    // 所属区域
    public function zones()
    {
        try {
            $data = ReportsRepo::zones();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 项目总览
    public function task_review()
    {
        try {
            $taskReview = ReportsRepo::taskReview();
            return message('获取成功', true, $taskReview);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 服务人效
    public function service_person()
    {
        $type = request()->get('type');
        $monthStart = request()->get('month_start') ?? '';
        $monthEnd = request()->get('month_end') ?? '';
        $types = collect(ReportsRepo::types())->pluck('code')->toArray();
        if (!in_array($type, $types)) {
            $type = ReportsRepo::TYPE_MONTH_SELECT;
        }
        try {
            $data = ReportsRepo::servicePerson($type, $monthStart, $monthEnd);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 平均服务用时
    public function avg_service_time()
    {
        $type = request()->get('type', '');
        $monthStart = request()->get('month_start') ?? '';
        $monthEnd = request()->get('month_end') ?? '';
        $types = collect(ReportsRepo::types())->pluck('code')->toArray();
        if (!in_array($type, $types)) {
            $type = ReportsRepo::TYPE_MONTH_SELECT;
        }
        try {
            $data = ReportsRepo::avgServiceTime($type, $monthStart, $monthEnd);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 区域工单统计
    public function zone_work_order()
    {
        $deptId = intval(request('dept_id', ''));
        try {
            $data = ReportsRepo::zoneWorkOrder($deptId);
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 人员工单统计
    public function person_work_order()
    {
        try {
            $data = ReportsRepo::personWorkOrder();
            return list_message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 关单率
    public function close_work_order()
    {
        try {
            $data = ReportsRepo::closeWorkOrder();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 客户满意度
    public function customer_satisfaction()
    {
        try {
            $data = ReportsRepo::customerSatisfaction();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 工单时效
    public function work_order_aging()
    {
        try {
            $data = ReportsRepo::workOrderAging();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 工单趋势
    public function work_order_tendency()
    {
        try {
            $data = ReportsRepo::workOrderTendency();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    // 问题反馈
    public function suggest()
    {
        try {
            $data = ReportsRepo::suggest();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function avg_response(Request $request)
    {
        try {
            $data = ReportsRepo::avgResponse();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function avg_response_detail(Request $request)
    {
        try {
            $data = ReportsRepo::avgResponseDetail();
            return list_message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function repeat_repair(Request $request)
    {
        try {
            $data = ReportsRepo::repeatRepair();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function repeat_repair_detail(Request $request)
    {
        try {
            $data = ReportsRepo::repeatRepairDetail();
            return list_message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function wo_receipt(Request $request)
    {
        try {
            $data = ReportsRepo::woReceipt();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function wo_receipt_detail(Request $request)
    {
        try {
            $data = ReportsRepo::woReceiptDetail();
            return list_message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}